#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<random>
#include<vector>

using namespace std;

vector<int> sortArray(vector<int>& nums);
void qsort(vector<int>& nums, int l, int r);
int getrandom(vector<int>& nums, int left, int right);



vector<int> sortArray(vector<int>& nums) {
    srand(time(NULL));
    qsort(nums, 0, nums.size()-1);
    return nums;
}

void qsort(vector<int>& nums, int l, int r) {
    if (l >= r) return;

    int key = getrandom(nums, l, r);
    int left = l - 1, right = r + 1, i = l;
    while (i < right) {
        if (nums[i] < key)
            swap(nums[i++], nums[++left]);
        else if (nums[i] == key)
            ++i;
        else
            swap(nums[--right], nums[i]);
    }
    qsort(nums, l, left);
    qsort(nums, right, r);
}

int getrandom(vector<int>& nums, int left, int right) {

    int r = rand();
    return nums[r % (right - left + 1) + left];
}

int main()
{
    vector<int> nums{ 5,2,3,1 };
    vector<int> ret = sortArray(nums);
    for (auto e : ret) {
        cout << e << " ";
    }
    return 0;
}